﻿//================================================================================
// Filename: LoggerPlugin.as
// Authour: Jacky Fong
// Description:
// Take API Log event, write to it text box
//
// Input Events
// - LoggerPlugin_Load
// - LoggerPlugin_Show
// - LoggerPlugin_SetPosition
// - KeyboardEvent.KEY_DOWN
//
// Output Events
// - LoggerPlugin_LoadComplete
//
// Listening to other Events
// - API_Log
//================================================================================

package
{
	import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.net.URLRequest;	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.ui.Keyboard;
	import IPlugin;
	import PluginManager;
	import PluginEvent;
	
	public class LoggerPlugin extends Sprite implements IPlugin
	{
		private static const _pluginName:String = "LoggerPlugin";
		private var _manager:PluginManager;
		private var _loader:Loader; // loader as container
		
		public function LoggerPlugin()
		{
			init();
		}
		
		public function get pluginName():String
		{
			return _pluginName;
		}
		
		public function init():void
		{
			trace( "Init " + _pluginName );
		}
		
		public function setManager( manager:PluginManager ):void
		{
			_manager = manager;
			_setupEventListeners();
		}		
		
		// listen to plugin's self operations
		private function _setupEventListeners():void
		{
			if( _manager == null ) return;
			
			_manager.addEventListener( "LoggerPlugin_Load", _onLoad );			
			_manager.addEventListener( "LoggerPlugin_Show", _onShow );
			_manager.addEventListener( "LoggerPlugin_SetPosition", _onSetPosition );
			
			_manager.addEventListener( "API_Log", _onLog );
			
			//_manager.addEventListener( KeyboardEvent.KEY_DOWN, _onKeyDown );
		}
		
       private function _loadComplete( evt:Event ):void
		{
			trace( this + " Load " + evt.target.content + " Complete: " + evt );
			_manager.dispatchEvent( new PluginEvent( "LoggerPlugin_LoadComplete" ) );			
        }
		
		private function _loadIoError( evt:IOErrorEvent ):void
		{
			trace( this + " Load IoError: " + evt );
		}
		
		private function _onKeyDown( evt:KeyboardEvent ):void
		{
			if( evt.keyCode == Keyboard.SPACE ) {
				_loader.visible = !_loader.visible;
				if( _loader.visible ) {
					_moveToTop();
				}
			}
		}
		
		private function _moveToTop():void
		{
			_manager.apiSetChildIndex( _loader, _manager.apiGetNumChildren() - 1 );
		}
		
		//==========================================================================
		// plugin API
		//==========================================================================		
		private function _onLoad( evt:PluginEvent ):void
		{
			trace( this + " _onLoad: " + evt );
			
			_loader = new Loader();
			_loader.visible = false;
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _loadComplete );			
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _loadIoError );
            var req:URLRequest = new URLRequest( evt.args.url );
            _loader.load( req );
			_manager.apiAddChild( _loader );
		}
		
		private function _onShow( evt:PluginEvent ):void
		{
			trace( this + " _onShow: " + evt );
			if( evt.args.show ) {
				_loader.visible = true;
				_moveToTop();
			}
			else {
				_loader.visible = false;
			}
		}
		
		private function _onSetPosition( evt:PluginEvent ):void
		{
			trace( this + " _onSetPosition: " + evt );
			_loader.x = evt.args.x;
			_loader.y = evt.args.y;
		}
		
		private function _onLog( evt:PluginEvent ):void
		{
			trace( this + " _onLog: " + evt );
			var log:DisplayObject = _loader.content
			log[ "txtLog" ].text += evt.args.msg + "\n";
		}		
	}
}